package com.jieshen.lesson2;

import com.jieshen.lesson2.utils.myAWTTools;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalcTest {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        //三个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(10);

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorLinstener(this));

        //一个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
        myAWTTools.windowClose(this);
    }

    public Calculator(){

    }
}

//监听器类
class MyCalculatorLinstener implements ActionListener{
    //需要在这个类中调用外部类
    //组合
    Calculator calculator = null;

    public MyCalculatorLinstener(Calculator calculator) {
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        calculator.num3.setText(""+(n1+n2));
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}
